package com.jingtuo.android.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

import com.jingtuo.android.R;
import com.jingtuo.android.common.CacheManager;
import com.jingtuo.android.common.app.BaseFragment;
import com.jingtuo.android.common.os.BitmapInfo;
import com.jingtuo.android.common.widget.ImageView;
import com.jingtuo.android.common.widget.ListAdapter;
import com.jingtuo.android.page.PicturePreviewPage;

public class PicturesFragment extends BaseFragment implements OnItemClickListener{

	private GridView gridView;
	
	private Adapter adapter;
	
	private ArrayList<BitmapInfo> data;
	
	private ArrayList<String> pictures;
	
	private CacheManager cacheManager;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		cacheManager = CacheManager.getInstance(getString(R.string.app_name));
		data= new ArrayList<BitmapInfo>();
		pictures = new ArrayList<String>();
		add("http://d.hiphotos.baidu.com/image/pic/item/9c16fdfaaf51f3def7f5c0c196eef01f3b2979cb.jpg");
		add("http://d.hiphotos.baidu.com/image/pic/item/b03533fa828ba61e134b092b4334970a304e5909.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/6c224f4a20a44623de2cf2f89a22720e0df3d742.jpg");
		add("http://f.hiphotos.baidu.com/image/pic/item/0eb30f2442a7d9334d09b3e9af4bd11372f00182.jpg");
		add("http://f.hiphotos.baidu.com/image/pic/item/b8389b504fc2d56270cb6a79e51190ef77c66c67.jpg");
		add("http://f.hiphotos.baidu.com/image/pic/item/e61190ef76c6a7ef739b5022fffaaf51f3de6671.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/d01373f082025aaf829583cff9edab64034f1a0a.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/3b292df5e0fe992573ab945436a85edf8db1710b.jpg");
		add("http://f.hiphotos.baidu.com/image/pic/item/29381f30e924b899fc0389326c061d950a7bf6f9.jpg");
		add("http://b.hiphotos.baidu.com/image/pic/item/91ef76c6a7efce1b6c5267d7ad51f3deb48f65b6.jpg");
		add("http://b.hiphotos.baidu.com/image/pic/item/7dd98d1001e93901f075113479ec54e736d19641.jpg");
		add("http://a.hiphotos.baidu.com/image/pic/item/b21bb051f81986189e64b88e48ed2e738bd4e69d.jpg");
		add("http://c.hiphotos.baidu.com/image/pic/item/a686c9177f3e6709ebe9e9e239c79f3df9dc55c5.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/4bed2e738bd4b31cf13b3ba685d6277f9e2ff8ac.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/d439b6003af33a876ef5e105c45c10385343b511.jpg");
		add("http://e.hiphotos.baidu.com/image/pic/item/500fd9f9d72a6059416e3e042a34349b033bbab6.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/b219ebc4b74543a91c5891c61c178a82b901147c.jpg");
		add("http://c.hiphotos.baidu.com/image/pic/item/b64543a98226cffc1c7a769cbb014a90f603ea48.jpg");
		add("http://a.hiphotos.baidu.com/image/pic/item/9f2f070828381f3031b9f8a0ab014c086e06f013.jpg");
		add("http://e.hiphotos.baidu.com/image/pic/item/f7246b600c3387449f5ece63530fd9f9d72aa012.jpg");
		add("http://d.hiphotos.baidu.com/image/pic/item/42a98226cffc1e178909451f4890f603738de96e.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/9d82d158ccbf6c812f9fe0e4be3eb13533fa4016.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/caef76094b36acaf7190b6e27ed98d1001e99cbf.jpg");
		add("http://a.hiphotos.baidu.com/image/pic/item/b219ebc4b74543a9824c33e21c178a82b901144c.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/d788d43f8794a4c26af46a210cf41bd5ac6e39e0.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/6a600c338744ebf8f4c50a09dbf9d72a6059a754.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/72f082025aafa40f7c1043d7a964034f79f019e1.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/7dd98d1001e939015fc5441079ec54e737d196dd.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/2934349b033b5bb5cf6b5b6434d3d539b700bcef.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/f9dcd100baa1cd1105290b85bb12c8fcc2ce2df3.jpg");
		add("http://f.hiphotos.baidu.com/image/pic/item/838ba61ea8d3fd1f2449db9f324e251f94ca5fd6.jpg");
		add("http://d.hiphotos.baidu.com/image/pic/item/7a899e510fb30f24719e6b91ca95d143ad4b031f.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/9f510fb30f2442a7beffb73bd343ad4bd01302be.jpg");
		add("http://e.hiphotos.baidu.com/image/pic/item/562c11dfa9ec8a13bf8a2e45f503918fa1ecc087.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/3801213fb80e7bec7aab797b2d2eb9389b506b32.jpg");
		add("http://d.hiphotos.baidu.com/image/pic/item/42166d224f4a20a40503fcc392529822720ed091.jpg");
		add("http://c.hiphotos.baidu.com/image/pic/item/f3d3572c11dfa9ec50a72e8d60d0f703908fc19b.jpg");
		add("http://h.hiphotos.baidu.com/image/pic/item/472309f790529822d57f9958d5ca7bcb0b46d4d9.jpg");
		add("http://e.hiphotos.baidu.com/image/pic/item/0b55b319ebc4b74567735512cdfc1e178b8215ca.jpg");
		add("http://c.hiphotos.baidu.com/image/pic/item/9e3df8dcd100baa127daaabf4510b912c9fc2ec4.jpg");
		add("http://e.hiphotos.baidu.com/image/pic/item/4a36acaf2edda3cce8d06f2403e93901213f92fa.jpg");
		add("http://b.hiphotos.baidu.com/image/pic/item/3ac79f3df8dcd1003ba18d84708b4710b9122f51.jpg");
		add("http://b.hiphotos.baidu.com/image/pic/item/241f95cad1c8a7860052900a6509c93d70cf50be.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/4b90f603738da977bfdeccb5b251f8198718e3bf.jpg");
		add("http://b.hiphotos.baidu.com/image/pic/item/b3fb43166d224f4ac9f608890bf790529822d139.jpg");
		add("http://c.hiphotos.baidu.com/image/pic/item/1c950a7b02087bf4a8391d77f0d3572c10dfcf53.jpg");
		add("http://a.hiphotos.baidu.com/image/pic/item/91529822720e0cf327d435610846f21fbe09aa0d.jpg");
		add("http://a.hiphotos.baidu.com/image/pic/item/91529822720e0cf327d435610846f21fbe09aa0d.jpg");
		add("http://b.hiphotos.baidu.com/image/pic/item/dc54564e9258d1092aff96fbd358ccbf6c814d5e.jpg");
		add("http://a.hiphotos.baidu.com/image/pic/item/9d82d158ccbf6c81708aa19abe3eb13533fa4007.jpg");
		add("http://f.hiphotos.baidu.com/image/pic/item/5ab5c9ea15ce36d33859dc3a38f33a87e950b15f.jpg");
		add("http://c.hiphotos.baidu.com/image/pic/item/a5c27d1ed21b0ef401bd152cdfc451da81cb3e6e.jpg");
		add("http://f.hiphotos.baidu.com/image/pic/item/3bf33a87e950352a830b08095143fbf2b2118b4c.jpg");
		add("http://e.hiphotos.baidu.com/image/pic/item/fc1f4134970a304eb9468dfbd3c8a786c9175c4e.jpg");
		add("http://d.hiphotos.baidu.com/image/pic/item/0b46f21fbe096b6375e7aa570e338744ebf8ac3f.jpg");
		add("http://g.hiphotos.baidu.com/image/pic/item/d009b3de9c82d15842ea74b0820a19d8bc3e4242.jpg");
		add("http://c.hiphotos.baidu.com/image/pic/item/43a7d933c895d143977d7e2271f082025aaf07a3.jpg");
		add("http://e.hiphotos.baidu.com/image/pic/item/e1fe9925bc315c6048da43e18fb1cb1349547774.jpg");
		add("http://c.hiphotos.baidu.com/image/pic/item/0824ab18972bd4073f7b33f479899e510fb3097e.jpg");
		add("http://d.hiphotos.baidu.com/image/pic/item/aa64034f78f0f736645053010855b319ebc41307.jpg");
		add("http://b.hiphotos.baidu.com/image/pic/item/38dbb6fd5266d016e8565121952bd40735fa3568.jpg");
		add("http://a.hiphotos.baidu.com/image/pic/item/cf1b9d16fdfaaf514be462b68e5494eef01f7ab0.jpg");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.pictures, container, false);
		gridView = (GridView)view.findViewById(R.id.pictures_gridview);
		adapter = new Adapter(getActivity(), data);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(this);
		return view;
	}
	
	class Adapter extends ListAdapter<BitmapInfo>{

		public Adapter(Context context, List<BitmapInfo> data) {
			super(context, data);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
//			if(convertView==null){
				convertView = View.inflate(context, R.layout.pictures_item, null);
//			}
			ImageView imageView = (ImageView)convertView;
			BitmapInfo bitmapInfo = data.get(position);
			bitmapInfo.setDstWidth(gridView.getColumnWidth());
			cacheManager.setImageView(imageView,bitmapInfo);
			return convertView;
		}
		
	}
	
	public void add(String path){
		pictures.add(path);
		BitmapInfo bitmapInfo = new BitmapInfo();
		bitmapInfo.setType(BitmapInfo.TYPE_URL);
		bitmapInfo.setPath(path);
		bitmapInfo.setIcon(true);
		data.add(bitmapInfo);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(getActivity(), PicturePreviewPage.class);
		Bundle extras = new Bundle();
		extras.putParcelableArrayList(PicturePreviewPage.EXTRA_PICTURES, data);
		extras.putInt(PicturePreviewPage.EXTRA_CURRENT_INDEX, position);
		intent.putExtras(extras);
		startActivity(intent);
	}
	
	
	
}
